<?php
require_once 'function.php';

$f = $_SERVER['REQUEST_METHOD'];
$a = $_GET['a'];
$b = $_GET['b'];

switch($a)
{
    case 'login':
        switch($b)
        {
            case 'index':
            {
                switch($f)
                {
                    case 'GET':
                        require_once('./login.html');
                        break;
                    case 'POST':
                        $username = $_POST['username'];
                        $password = $_POST['password'];

                        $sql = "select * from user where name='$username' and passwd='$password'";

                        $result = query_select($sql);
                        $result = end($result);
                        if (empty($result))
                        {
                            alert_form("登陆失败: 用户名或密码错误", "./index.php?a=login&b=index");
                        }
                        else
                        {
                            alert_form("登陆成功", "./index.php?a=login&b=index");
                        }

                        break;
                    default:
                        break;
                }
            }
            break;

            case 'register':
                {
                    switch($f)
                    {
                        case 'GET':
                            require_once('./register.html');
                            break;
                        case 'POST':
                            $username = $_POST['username'];
                            $password = $_POST['password'];

                            $sql = "select * from user where name='$username'";
                            $result = query_select($sql);
                            #var_dump($result);
                            if (!empty($result))
                            {
                                return alert_ajax("用户名已存在", "./index.php?a=login&b=register");
                            }

                            $sql = "insert into user(name, passwd) values('$username', '$password')";
                            $result = query($sql);
                            if ($result == false)
                            {
                                alert_ajax("注册失败", "./index.php?a=login&b=register");
                            }
                            else
                            {
                                alert_ajax("注册成功", "./index.php?a=login&b=index");
                            }
                            break;
                        default:
                            break;
                    }
                }
                break;


        default:
            break;
        }
        break;
    default:
        break;
}
?>